﻿using System.Windows;
using Baosight.Platform.Core.UI.Controls;
using System.Collections.Generic;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;
using System.Windows.Threading;
using System;
using System.Windows.Controls;
using Baosight.ZNH.ZMCommon.ChildWindows;
using System.Linq;

namespace Baosight.ZNH.ZMCM.UI.ChildWindows
{
    public partial class MaterialBasicInfo : EFViewCmes
    {
        public MaterialBasicInfo(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);           
        }

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    base.Mode = OperationMode.Insert;
                    this.fdMaterialStatus.Value = "在用";
                    this.SetMaterialType(null);
                    break;
                case "update":
                    base.Mode = OperationMode.Update;
                    this.fdMaterialTypeId.IsEnabled = false;//物资分类不可编辑
                    this.fdMaterialLastCode.IsEnabled = false;//物资尾码不可编辑
                    this.fdPrice.IsEnabled = false;
                    //this.fdModuleName.IsReadOnly = true;
                    break;
                case "detail":
                    this.Commit.Visibility = Visibility.Collapsed;
                    BaseTools.EFDataFieldNotEnabled(this.spWindows.GetChildObjects<EFDataField>(null, true));
                    break;
                default:
                    break;
            }
        }

        private void SetMaterialType(string _args)
        {
            EIInfo eIInfo = new EIInfo();
            eIInfo.Attributes.Add(EIConstants.ServiceNameKey, "ZMCM01");
            eIInfo.Attributes.Add(EIConstants.MethodNameKey, "aaa");
        }
        
        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            base.OnCommitButtonClick("ZMCM02", sender, e);
        }

        public override void DoCommitCompleted(object sender, Platform.Core.ServiceModel.DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else if (!inInfo.GetDetailMsg().Equals(""))
            {
                this.OpenEFWindowAlert(inInfo.GetDetailMsg(), true);//操作成功，消息提醒
                EFWindow window = this.Parent as EFWindow;
                window.DialogResult = true;
                window.Close();
            }
            else
            {
                //操作成功关闭新增页面
                //此处可以扩展是否连续新增
                EFWindow window = this.Parent as EFWindow;
                window.DialogResult = true;
                window.Close();
            }
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        private void fdMaterialTypeId_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFDataField field = sender as EFDataField;
            CmesCustomView picker = field.Content as CmesCustomView;
            if (picker.ItemObject != null)
            {
                if (!picker.ItemObject.Equals(""))
                {
                    string fdMaterialTypeInnerCode = picker.ItemObject.GetType().GetProperty("fdTypeInnerCode").GetValue(picker.ItemObject, null) as string;
                    string fdTypeName = picker.ItemObject.GetType().GetProperty("fdTypeName").GetValue(picker.ItemObject, null) as string;
                    string fdTypeAllCode = picker.ItemObject.GetType().GetProperty("fdTypeAllCode").GetValue(picker.ItemObject, null) as string;

                    this.fdMaterialTypeInnerCode.Value = fdMaterialTypeInnerCode;
                    this.fdMaterialType.Value = fdTypeName;
                }
                else
                {
                    return;
                }
            } 
        }

        //private void fdMaterialTypeId_Loaded(object sender, RoutedEventArgs e)
        //{
        //    EFDataField field = sender as EFDataField;
        //    EFComboBox box = field.Content as EFComboBox;
        //    var result = from c in box.Items where c.GetType().GetProperty("fdKeyId").GetValue(c, null).ToString().Equals(field.Value.ToString()) select c;

        //    if (result.Count() > 0)
        //    {
        //        object Item = result.First();
        //        this.fdMaterialTypeInnerCode.Value = Item.GetType().GetProperty("fdTypeInnerCode").GetValue(Item, null);
        //        this.fdMaterialType.Value = Item.GetType().GetProperty("fdTypeName").GetValue(Item, null);
        //    }
        //}

       
    }

}
